using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ray : MonoBehaviour
{
    private GameObject enemy_in = null;
    private LineRenderer laser1;
    public GameObject energyBar;
    public int energy_max = 100;
    public float energy = 100;
    public Slider slider;
    public bool isFire = true;
    private void Start()
    {
        energyBar = GameObject.Find("energyBar");
        //获取血条的silder
        slider = energyBar.transform.GetChild(0).gameObject.GetComponent<Slider>();
        laser1 = gameObject.GetComponent<LineRenderer>();
    }
    void Update()
    {
        if (isFire)
        {
            enemy_in = auto_detect(100);
            if (enemy_in != null)
            {
                Vector3 enemy_position = new Vector3(enemy_in.transform.position.x, transform.position.y, enemy_in.transform.position.z);
                gameObject.transform.LookAt(enemy_position);
                energy -= 0.01f;
                slider.value = (float)energy / energy_max;
                fire();
                if (energy <= 0)
                {
                    isFire = false;
                }
            }
        }
        else
        {
            energy += 0.1f;
            slider.value = (float)energy / energy_max;
            if (energy >= 100)
            {
                isFire = true;
            }
        }
    }
    private GameObject auto_detect(float _fire_range_)
    {
        //自动检测敌人方法,参数为射程

        float distance_min = 100;
        //初始化最近敌人距离

        GameObject enemy_target = null;
        //初始化敌人目标  

        GameObject[] enemy_array = GameObject.FindGameObjectsWithTag("enemy");
        //找到所有标签为enemy的游戏对象,存储在数组enemy_array中

        foreach (GameObject element in enemy_array)
        {
            //使用foreach循环遍历enemy_array数组中的所有游戏对象

            float distance_now = Vector3.Distance(transform.position, element.transform.position);
            //计算当前游戏对象与敌人的距离  

            if (distance_now < _fire_range_)
            {
                //如果当前计算出的距离小于射程,执行{}中的语句             

                if (distance_now < distance_min)
                {
                    //如果当前距离小于之前记录的最近距离,执行{}中的语句

                    distance_min = distance_now;
                    //更新最近距离 
                    enemy_target = element;
                    //更新敌人目标
                }
            }
        }
        return enemy_target;
        //返回敌人目标
    }

    private void fire()
    {
        //fire()方法,负责发射激光  
        if (enemy_in != null)
        //如果enemy_in不等于null,执行{}中的语句  
        {
            transform.LookAt(enemy_in.transform);
            //状态机朝向enemy_in          
            //发射激光_  
            Vector3[] positions = new Vector3[2];
            //声明一个Vector3数组positions,大小为2
            Vector3 hit_now = ray_collision(gameObject.transform.Find("firepoint").transform.position, enemy_in.transform.position);
            //使用ray_collision()方法进行射线检测,返回撞击点坐标hit_now
            transform.LookAt(hit_now);
            //状态机朝向hit_now
            positions[0] = gameObject.transform.Find("firepoint").transform.position;
            //将发射点坐标赋值给positions[0]
            positions[1] = hit_now;
            //将hit_now赋值给positions[1]        
            laser1.startWidth = 0.02f;
            //设置laser1的startWidth为0.02
            laser1.endWidth = 0.02f;
            //设置laser1的endWidth为0.02
            laser1.positionCount = positions.Length;
            //设置laser1的positionCount为positions数组的长度  
            laser1.SetPositions(positions);
            //调用SetPositions()方法,设置laser1的发射点和终点坐标为positions数组的值
        }
        else
        {
            //否则,如果enemy_in等于null,执行{}中的语句  
            Vector3[] positions = new Vector3[2];
            //声明一个Vector3数组positions,大小为2
            positions[0] = gameObject.transform.Find("firepoint").transform.position;
            //将发射点坐标赋值给positions[0]             
            positions[1] = gameObject.transform.Find("firepoint").transform.position;
            //将发射点坐标赋值给positions[1]          
            laser1.SetPositions(positions);
            //调用SetPositions()方法,设置laser1的发射点和终点坐标为positions数组的值         
        }
    }
    private Vector3 ray_collision(Vector3 start_position, Vector3 target_position)
    {
        Vector3 direction_now = target_position - start_position;
        float maxDistance = Vector3.Distance(start_position, target_position);
        //这里的direction_now指的是方向，他不是终点坐标，所以需要终点减去起点得到射线的方向
        Ray ray = new Ray(start_position, direction_now);
        RaycastHit hit;
        LayerMask mask = LayerMask.GetMask("enemy_layer");
        bool is_hit = Physics.Raycast(ray, out hit, maxDistance, mask);
        Debug.DrawRay(start_position, direction_now, Color.green);
        if (is_hit == true)
        {
            GameObject hit_now = hit.collider.gameObject;
            if (hit_now.tag == "enemy")
            {
                enemy enemyScript = hit_now.GetComponent<enemy>();
                enemyScript.hp -= 0.5f;  // 访问enemy脚本的hp并减10
                hit.collider.gameObject.GetComponent<Renderer>().material.color = Color.red;
                Vector3 hit_position = new Vector3(hit.point.x, transform.position.y, hit.point.z);
                return hit_position;
            }
            else
            {

                return target_position;
            }
        }
        else
        {
            return target_position;
        }
    }
}
